<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
    <el-table :data="temdictionaryTypes" highlight-current-row v-loading="listLoading" @row-dblclick="openDrawer" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>

      <el-table-column prop="id" label="编号" width="200" ></el-table-column>

      <el-table-column prop="name" label="数据字典名" width="200" ></el-table-column>

      <el-table-column prop="sn" label="数据字典编号" ></el-table-column>


      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>


    <!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination
          layout="total, sizes, prev, pager, next"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
          :page-sizes="[5, 8, 12, 20]"
          :current-page="currentPage"
          :page-size="pageSize"
          :total="total"
          style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">

				<el-form-item label="数据字典名" prop="name">
					<el-input v-model="editForm.name" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="数据字典编号" prop="sn">
          <el-input v-model="editForm.sn" auto-complete="off"></el-input>
        </el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

    <!--// 抽屉-->
    <el-drawer
        :title="drawerTitle"
        :visible.sync="drawer"
        :before-close="handleClose">
      <el-table :data="temdictionaryDetails" highlight-current-row v-loading="drawerListLoading" @selection-change="drawerSelsChange" style="width: 100%;">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column type="index" label="编号"  width="120">
        </el-table-column>


        <el-table-column prop="name" label="明细名"></el-table-column>


        <el-table-column width="150">
          <template slot="header" slot-scope="scope">
            <label>选择操作|</label>
            <el-button type="primary" size="small" @click="drawerHandleAdd()">新增</el-button>
          </template>
          <template scope="scope">
            <el-button size="small" @click="drawerHandleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="drawerHandleDel(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-col :span="24" class="toolbar">
        <el-button type="danger" @click="drawerBatchRemove" :disabled="this.drawerSels.length===0">批量删除</el-button>
        <el-pagination
            layout="total, sizes, prev, pager, next"
            @current-change="drawerHandleCurrentChange"
            @size-change="drawerHandleSizeChange"
            :page-sizes="[5, 8, 12, 20]"
            :current-page="drawerQuery.currentPage"
            :page-size="drawerQuery.pageSize"
            :total="drawerTotal"
            style="float:right;">
        </el-pagination>
      </el-col>
    </el-drawer>

<!--    抽屉编辑模态框-->
    <el-dialog :title="drawerEditTitle" :visible.sync="drawerEditFormVisible" :close-on-click-modal="false">
      <el-form :model="drawerEditForm" label-width="80px" :rules="drawerEditFormRules" ref="editForm">

        <el-form-item label="明细" prop="name">
          <el-input v-model="drawerEditForm.name" auto-complete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="drawerEditFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="drawerEditSubmit" :loading="drawerListLoading">提交</el-button>
      </div>
    </el-dialog>

	</section>
</template>

<script>

	import util from "../../common/js/util";

  export default {
		data() {
			return {
				filters: {
					keyword: ''
				},
        temdictionaryTypes: [],
				total: 0,
        currentPage: 1,
        pageSize: 5,
				listLoading: false,
				sels: [],//列表选中列

				editFormVisible: false, //编辑界面是否显示
				editLoading: false,

        drawer:false, // 抽屉
        drawerTitle:'',
        temdictionaryDetails:[],
        drawerListLoading:false,
        drawerEditFormVisible: false, // 抽屉的编辑界面是否显示
        drawerEditTitle:'????',
        drawerSels: [],//列表选中列,

        drawerTotal: 0,

        drawerTemdictionaryTypes: [],

        drawerQuery:{
          currentPage: 1,
          pageSize: 5,
          types_id:null
        },



				editFormRules: {
					name: [
						{ required: true, message: '请输入数据字典名', trigger: 'blur' }
					],
          sn: [
            { required: true, message: '请输入数据字典编号', trigger: 'blur' }
          ]
				},
        drawerEditFormRules: {
          name: [
            { required: true, message: '请输入明细', trigger: 'blur' }
          ]
        },
				//编辑界面数据
        title: '',
        cities: [],
				editForm: {
          id:null,
          name:'',
          sn:'',
          children:[]
				},
        drawerEditForm:{
          id:null,
          name:'',
          types_id:null
        }
			}
		},
		methods: {
      // 页码发生改变的监听
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getTemdictionaryTypes();
			},
      // 每页展示条数发生改变的监听
      handleSizeChange(val){
        this.pageSize = val;
        this.currentPage = 1;
        this.getTemdictionaryTypes();
      },
      // 高级查询方法
      search(){
        this.currentPage = 1;
        this.getTemdictionaryTypes();
      },
			//获取数据字典列表
			getTemdictionaryTypes() {
				let para = {
          currentPage: this.currentPage,
          pageSize: this.pageSize,
          keyword: this.filters.keyword
				};
				this.listLoading = true;
        this.$http.post("/temdictionaryType",para)
          .then(result=>{
            this.listLoading = false;
            this.temdictionaryTypes = result.data.rows;
            this.total = result.data.total;
          })
          .catch(result=>{
            this.listLoading = false;
            this.$message({
              message: '系统繁忙，请稍后再试',
              type: 'error'
            });
          })
			},
      //批量删除
      batchRemove: function () {
        var ids = this.sels.map(item => item.id);
        this.$confirm('确认删除选中记录吗？', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          this.$http.patch("/temdictionaryDetail/types_id", ids)
            .then(childResult=>{
              this.$http.patch("/temdictionaryType",ids)
                  .then(result=>{
                    this.listLoading = false;
                    if(result.data.success){
                      this.$message({
                        message: result.data.message,
                        type: 'success'
                      });
                    }else {
                      this.$message({
                        message: result.data.message,
                        type: 'error'
                      });
                    }

                    this.currentPage = 1;
                    this.getTemdictionaryTypes();
                  })
                  .catch(result=>{
                    this.listLoading = false;
                    this.$message({
                      message: '系统繁忙,请稍后再试',
                      type: 'error'
                    });
                  })
            })
        }).catch(() => {

        });
      },
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
          this.$http.delete("/temdictionaryDetail/types_id/"+row.id)
            .then(childResult=>{
              this.$http.delete("/temdictionaryType/"+row.id)
                .then(result=>{
                  this.listLoading = false;
                  if(result.data.success){
                    this.$message({
                      message: result.data.message,
                      type: 'success'
                    });
                  }else {
                    this.$message({
                      message: result.data.message,
                      type: 'error'
                    });
                  }
                  this.currentPage = 1;
                  this.getTemdictionaryTypes();
                })
                .catch(result=>{
                  this.listLoading = false;
                  this.$message({
                    message: '系统繁忙,请稍后再试',
                    type: 'error'
                  });
                })
            })
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
        this.title = '编辑';
				this.editFormVisible = true;
        this.passwordShow = true;
				this.editForm = Object.assign({}, row);

			},
      drawerHandleEdit: function (index, row) {
        this.drawerEditTitle = row.name;
				this.drawerEditFormVisible = true;
				this.drawerEditForm = Object.assign({}, row);

			},
			//显示新增界面
			handleAdd: function () {
        this.title = '新增';
        this.passwordShow = false,
				this.editFormVisible = true;
				this.editForm = {
          id:null,
          name:'',
          sn:''
				};
			},
      drawerHandleAdd: function () {
        this.drawerEditTitle = '新增';
        this.drawerEditFormVisible = true;
        this.drawerEditForm = {
          id:null,
          name:'',
          types_id:null
        };
      },
			//编辑提交
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
              // 封装
							let para = Object.assign({}, this.editForm);
              this.$http.put("/temdictionaryType",para)
                  .then(result=>{
                    this.editLoading = false;
                    if(result.data.success){
                      this.$message({
                        message: result.data.message,
                        type: 'success'
                      });
                    }else {
                      this.$message({
                        message: result.data.message,
                        type: 'error'
                      });
                    }
                    this.editFormVisible = false;
                    this.currentPage = 1;
                    this.getTemdictionaryTypes();
                  })
                  .catch(result=>{
                    this.editLoading = false;
                    this.$message({
                      message: '系统繁忙,请稍后再试',
                      type: 'error'
                    });
                  })
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
      drawerSelsChange: function (sels) {
        this.drawerSels = sels;
      },
      // 打开抽屉
      openDrawer(row){
        this.drawerQuery.currentPage = 1;
        this.drawerQuery.types_id = row.id;
        this.drawer = true;
        this.drawerTitle = row.name;
        this.getTemdictionaryDetails();
      },
      // 关闭抽屉
      handleClose(done) {
        this.$confirm('确认关闭？')
            .then(_ => {
              done();
            })
            .catch(_ => {});
      },
      // 页码发生改变的监听
      drawerHandleCurrentChange(val) {
        this.drawerQuery.currentPage = val;
        this.getTemdictionaryDetails();
      },
      // 每页展示条数发生改变的监听
      drawerHandleSizeChange(val){
        this.drawerQuery.pageSize = val;
        this.drawerQuery.currentPage = 1;
        this.getTemdictionaryDetails();
      },
      // 子集数据编辑提交
      drawerEditSubmit: function () {
        this.$refs.editForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.editLoading = true;
              // 封装
              let para = Object.assign({}, this.drawerEditForm);
              para.types_id = this.drawerQuery.types_id;
              this.$http.put("/temdictionaryDetail",para)
                  .then(result=>{
                    this.editLoading = false;
                    if(result.data.success){
                      this.$message({
                        message: result.data.message,
                        type: 'success'
                      });
                    }else {
                      this.$message({
                        message: result.data.message,
                        type: 'error'
                      });
                    }
                    this.editFormVisible = false;
                    this.currentPage = 1;
                    this.drawerEditFormVisible = false;
                    this.getTemdictionaryDetails();
                  })
                  .catch(result=>{
                    this.editLoading = false;
                    this.$message({
                      message: '系统繁忙,请稍后再试',
                      type: 'error'
                    });
                  })
            });
          }
        });
      },
      // 获取子集数据
      getTemdictionaryDetails(){

        let para = Object.assign({}, this.drawerQuery);
        console.log(para);
        this.$http.post('/temdictionaryDetail/',para)
            .then(result=>{
              this.temdictionaryDetails = result.data.rows;
              this.drawerTotal = result.data.total;
            })

      },
      // 删除子集数据
      drawerHandleDel: function (index, row) {
        this.$confirm('确认删除该记录吗?', '提示', {
          type: 'warning'
        }).then(() => {
          this.drawerListLoading = true;
          this.$http.delete("/temdictionaryDetail/"+row.id)
              .then(result=>{
                this.drawerListLoading = false;
                if(result.data.success){
                  this.$message({
                    message: result.data.message,
                    type: 'success'
                  });
                }else {
                  this.$message({
                    message: result.data.message,
                    type: 'error'
                  });
                }
                this.currentPage = 1;
                this.getTemdictionaryDetails();
              })
              .catch(result=>{
                this.drawerListLoading = false;
                this.$message({
                  message: '系统繁忙,请稍后再试',
                  type: 'error'
                });
              })
        }).catch(() => {

        });
      },
      drawerBatchRemove: function () {
        var ids = this.drawerSels.map(item => item.id);
        this.$confirm('确认删除选中记录吗？', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          this.$http.patch("/temdictionaryDetail",ids)
              .then(result=>{
                this.listLoading = false;
                if(result.data.success){
                  this.$message({
                    message: result.data.message,
                    type: 'success'
                  });
                }else {
                  this.$message({
                    message: result.data.message,
                    type: 'error'
                  });
                }
                this.currentPage = 1;
                this.getTemdictionaryDetails();
              })
              .catch(result=>{
                this.listLoading = false;
                this.$message({
                  message: '系统繁忙,请稍后再试',
                  type: 'error'
                });
              })
        }).catch(() => {

        });
      },


		},
		mounted() {
			this.getTemdictionaryTypes();
		}
	}

</script>

<style scoped>

</style>